import { message } from 'antd';
import { RcFile } from 'antd/es/upload/interface';

import { MIN_IMAGE_EDGE } from '../constant';

export const getImgDimension = (
  url: string
): Promise<{ width: number; height: number }> => {
  return new Promise((resolve, reject) => {
    const image = new Image();

    image.onload = (): void => {
      resolve({
        width: image.width,
        height: image.height
      });
    };

    image.onerror = (err): void => {
      message.error('Wrong image format.');
      reject(err);
    };

    image.src = url;
  });
};

export const createURLForFile = (file: RcFile): string => {
  const nativeURL = window.URL || window.webkitURL;
  if (!file || !nativeURL) {
    return;
  }

  return nativeURL.createObjectURL(file); // base 64/ md5 压缩
};

export const hexToRgb = (hex: string): { r: number; g: number; b: number } => {
  const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result
    ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
      }
    : { r: 0, g: 0, b: 0 };
};

export const hexToRgba = (hex: string, transparency): string => {
  const { r, g, b } = hexToRgb(hex);

  return `rgba(${r}, ${g}, ${b}, ${transparency})`;
};

export const handleImageValuesChange = (
  values,
  canvasWidth,
  canvasHeight
): void => {
  values.data.image.isCropped = false;
  values.data.image.originSrc = values.src;
  values.data.image.cropped = null;
  values.data.rotation = 0;
  values.angle = 0;

  if (values.src) {
    // 如果是上传图片，需要设置成和画布一样大
    const { width, height } = values;
    if (width > height) {
      values.width = canvasWidth;
      values.height = (canvasWidth * height) / width;
    } else {
      values.width = (canvasHeight * width) / height;
      values.height = canvasHeight;
    }

    values.left = 0 + values.width / 2;
    values.top = 0 + values.height / 2;
  } else {
    // 删除图片则重置数值
    values.width = 0;
    values.height = 0;
    values.left = 0;
    values.top = 0;
    values.data.image.fileKey = undefined;
  }
};

export const handleProductImageValuesChange = (values): void => {
  values.data.rotation = 0;
  values.angle = 0;

  if (values.src) {
    values.left = 0 + values.width / 2;
    values.top = 0 + values.height / 2;
  } else {
    // 删除图片则重置数值
    values.width = 0;
    values.height = 0;
    values.left = 0;
    values.top = 0;
    values.data.ratio = [1, 1]; // 重置为 1:1
    values.data.image.cropped = null;
    values.data.image.isCropped = false;
    values.data.image.originSrc = values.src;
    values.data.image.fileKey = undefined;
  }
};

export const ifUploadImageSortEdgeTooSmallAfterResize = (
  width: number,
  height: number
): boolean => {
  const maxEdge = Math.max(width, height);
  const minEdge = Math.min(width, height);

  const maxEdgeResizeTarget = 1000; // 最大边需要缩放到的指定尺寸，目前product裁剪弹窗和画布最小边都是1000，按照这个来算

  const minEdgeAfterResize = Math.ceil(
    (minEdge / maxEdge) * maxEdgeResizeTarget
  );

  return minEdgeAfterResize < MIN_IMAGE_EDGE;
};
